//
//  STab.m
//  EasyRetail
//
//  Created by Samuel on 12/18/15.
//
//

#import "STab.h"
#import "SDefine.h"
#import "SAnimation.h"
@interface STab ()

//@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic, strong)UIButton *recordButton;
@property (nonatomic, strong)UIView *bottomView;
@end
@implementation STab
{
    STabButton *moveLastButton;
    BOOL is_Set_Button_No_Delegate;
    BOOL isFirstTapButton;
    NSMutableArray *buttonArray;
}

-(void)awakeFromNib
{
    self.stringArray = [NSMutableArray array];
}
+ (STab*)shareSTabInitializationInArray:(NSMutableArray<NSString*>*)array
                               andFrame:(CGRect)frames;
{
    STab *stab = [[STab alloc]initWithFrame:frames];
    stab.stringArray = [[NSMutableArray alloc]init];
    [stab.stringArray addObjectsFromArray:array];
    return stab;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setBackgroundColor:[UIColor whiteColor]];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}
- (void)setDelegate:(NSObject<STabDelegate> *)delegate
{
    if (delegate != nil) {
        _delegate = delegate;
    }
    if (self.stringArray.count <1 || !self.stringArray) {
        return;
    }
    if (!self.scrollView) {
        self.scrollView = [[UIScrollView alloc]init];
        self.scrollView.showsHorizontalScrollIndicator = NO;
        ___setWidth(self.scrollView, ___width(self));
        ___setHeight(self.scrollView, ___height(self));
        [self.scrollView setBackgroundColor:[UIColor whiteColor]];
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, ___height(self)-.5, ___width(self), .5)];
        [line setBackgroundColor:[UIColor lightGrayColor]];
        [self addSubview:self.scrollView];
        [self addSubview:line];
        
    }
    for (UIView *views in self.scrollView.subviews) {
        [views removeFromSuperview];
    }
    float allLong = [self creatButtons];
    [self setOpenBottomView:YES];
    [self.scrollView setContentSize:CGSizeMake(((allLong<=___width(self))?___width(self):allLong), ___height(self))];
}
- (float)creatButtons
{
    BOOL isMore = NO;
    float allLongOne = 0;
    NSMutableArray *buttonArr = [NSMutableArray array];
    NSMutableArray *sizeArr = [NSMutableArray array];
    for (int i = 0;i<self.stringArray.count;i++) {
        NSString *getString = self.stringArray[i];
        CGSize getStringSize = [getString boundingRectWithSize:CGSizeMake(___width(self), 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil].size;
        [sizeArr addObject:NSStringFromCGSize(getStringSize)];
        allLongOne = allLongOne +getStringSize.width+40;
    }
    if (allLongOne <=___width(self)) {
        isMore = YES;
        [sizeArr removeAllObjects];
        float getOne = ___width(self)/self.stringArray.count;
        CGSize sizeOne = CGSizeMake(getOne, 0);
        for (int times = 0; times < self.stringArray.count; times++) {
            [sizeArr addObject:NSStringFromCGSize(sizeOne)];
        }
    }
    float allLong = 0;
    for (int k = 0; k < sizeArr.count; k++) {
        CGSize getStringSize = CGSizeFromString(sizeArr[k]);
        STabButton *buttons = [STabButton buttonWithType:UIButtonTypeCustom];
        ___setWidth(buttons, getStringSize.width + ((isMore)?0:40));
        ___setHeight(buttons, ___height(self));
        ___setLeft(buttons, allLong)
        allLong = allLong + ___width(buttons);
        [buttons setTitle:self.stringArray[k] forState:UIControlStateNormal];
        [buttons setTitleColor:___color_Value(0x848583, 1) forState:UIControlStateNormal];
        [buttons setTitleColor:(self.sColor)?self.sColor:___color_Value(0x848583, 1) forState:UIControlStateSelected];
        [buttons.titleLabel setFont:((self.sFont)?self.sFont:[UIFont systemFontOfSize:12])];
        [buttons setXPoint:allLong];
        [buttons setTag:k];
        [buttons addTarget:self action:@selector(buttonSelect:) forControlEvents:UIControlEventTouchDown];
        [self.scrollView addSubview:buttons];
        [buttonArr addObject:buttons];
        if (k==0) {
            isFirstTapButton = YES;
            [self buttonSelect:buttons];
        }

    }
    
    for (STabButton *butn in buttonArr) {
        if (allLong - butn.xPoint <= ___width(self)/2) {
            moveLastButton = butn;
            break;
        }
    }
    buttonArray = buttonArr;
    return allLong;
}
-(void)setOpenBottomView:(BOOL)openBottomView
{
    _openBottomView = openBottomView;
    if (_openBottomView) {
        if (!self.bottomView) {
            self.bottomView = [[UIView alloc]init];
            [self.bottomView setBackgroundColor:(self.sColor)?self.sColor:[UIColor blackColor]];
            ___setWidth(self.bottomView, ___width(self.recordButton));
            ___setHeight(self.bottomView, 2);
            ___setLeft(self.bottomView, ___left(self.recordButton));
            ___setTop(self.bottomView, ___bottom(self.recordButton)-___height(self.bottomView)-.5);
        }
        [self.scrollView addSubview:self.bottomView];
        return;
    }
    [self.bottomView removeFromSuperview];
    
}
- (void)buttonSelect:(STabButton*)sender
{
    if (self.recordButton == sender) {
        if (self.openBottomView && self.openDismissSelect) {
            [self.bottomView setHidden:(sender.selected)?YES:NO];
        }
        if (self.openDismissSelect) {
            sender.selected = (sender.selected)?NO:YES;
        }
        return;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(STab:didSelectRowAtIndex:)] && is_Set_Button_No_Delegate==NO && isFirstTapButton == NO)
    {
        [self.delegate STab:self didSelectRowAtIndex:sender.tag];
    }
    isFirstTapButton = NO;
    sender.selected = YES;
    self.recordButton.selected = NO;
    self.recordButton = sender;
    if (self.openBottomView) {
        [UIView animateWithDuration:0.3 animations:^{
            ___setLeft(self.bottomView, ___left(sender));
            ___setWidth(self.bottomView, ___width(sender));
        }];
    }
    float xPoint = ___left(sender)-___width(self)/2+___width(sender)/2;
    if (sender.xPoint<=___width(self)/2) {
        xPoint = 0;
        [self.scrollView setContentOffset:CGPointMake(xPoint,0) animated:YES];
        return;
    }
    if (sender.xPoint >= moveLastButton.xPoint) {
        [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentSize.width-___width(self),0) animated:YES];
        return;
    }
    [self.scrollView setContentOffset:CGPointMake(xPoint,0) animated:YES];
}
- (void)setAllButtonInteractionEnabled:(BOOL)open;
{
    for (UIButton *buttons in buttonArray) {
        [buttons setUserInteractionEnabled:open];
    }
}

/**
 *  重载数据
 */
- (void)reloadData;
{
    [self setDelegate:nil];
}
/**
 *  跳转到某个view
 *
 *  @param index
 */
- (void)setThePageToIndex:(NSInteger)index;
{
    is_Set_Button_No_Delegate = YES;
    for (UIView *getView in self.scrollView.subviews) {
        if ([getView isKindOfClass:[STabButton class]] && getView.tag==index) {
            [self buttonSelect:(STabButton*)getView];
        }
    }
    is_Set_Button_No_Delegate = NO;
}


@end
@implementation STabButton

@end